<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时聊天室</title>
    <style>
    /* 现代聊天主题变量 */
    :root {
        --primary: #2563eb;
        --primary-light: #eff6ff;
        --bg: #f8fafc;
        --message-in: #f1f5f9;
        --message-out: #2563eb;
        --text-light: #64748b;
        --border: #e2e8f0;
    }
        :root {
            --primary-color: #2563eb;
            --primary-light: #eff6ff;
            --bg-color: #f8fafc;
            --message-bg: #ffffff;
            --system-msg-color: #64748b;
            --border-color: #e2e8f0;
        }
        body {
            font-family: 'Inter', sans-serif;
            max-width: 1024px;
            margin: 0 auto;
            padding: 2rem;
            background-color: var(--bg-color);
            line-height: 1.5;
        }
        #loginForm {
            background: white;
            padding: 3rem;
            border-radius: 1.5rem;
            box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -4px rgba(0,0,0,0.1);
            margin: 8rem auto;
            max-width: 32rem;
            transition: transform 0.3s ease;
            text-align: center;
        }
        #loginForm:hover {
            transform: translateY(-5px);
        }
        #chatContainer {
            display: none;
            background: white;
            border-radius: 1rem;
            box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -2px rgba(0,0,0,0.1);
            padding: 2.5rem;
        }
        #messages {
            height: 60vh;
            border: 1px solid var(--border-color);
            background: #f1f5f9;
            border-radius: 0.75rem;
            margin: 1.5rem 0;
            padding: 1.5rem;
            overflow-y: auto;
            scrollbar-width: thin;
            scrollbar-color: var(--primary-color) #e5e7eb;
        }
        #messages::-webkit-scrollbar {
            width: 8px;
        }
        #messages::-webkit-scrollbar-track {
            background: #e5e7eb;
            border-radius: 4px;
        }
        #messages::-webkit-scrollbar-thumb {
            background: var(--primary-color);
            border-radius: 4px;
        }
        .input-group {
            display: flex;
            gap: 1rem;
            align-items: center;
        }
        #messageInput {
            flex: 1;
            padding: 0.875rem 1.25rem;
            border: 1px solid var(--border-color);
            border-radius: 1.5rem;
            font-size: 0.875rem;
            transition: border-color 0.3s ease;
        }
        #messageInput:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }
        button {
            padding: 0.875rem 1.5rem;
            border: none;
            border-radius: 1.5rem;
            background: var(--primary-color);
            color: white;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 0.875rem;
            font-weight: 500;
        }
        button:hover {
            background: #1d4ed8;
            transform: translateY(-1px);
        }
        .emoji-btn, .media-btn {
            padding: 0.875rem;
            background: var(--primary-light);
            color: var(--primary-color);
            display: flex;
            align-items: center;
        }
        .emoji-btn:hover, .media-btn:hover {
            background: #dbeafe;
        }
        .system-msg {
            color: var(--text-light);
            font-size: 0.8rem;
            margin: 1.5rem 0;
            text-align: center;
            position: relative;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .system-msg::before, .system-msg::after {
            background: #cbd5e1;
            width: 25%;
        }
        .system-msg::before, .system-msg::after {
            content: '';
            position: absolute;
            top: 50%;
            width: 30%;
            height: 1px;
            background: #e2e8f0;
        }
        .system-msg::before {
            left: 0;
        }
        .system-msg::after {
            right: 0;
        }
        .user-msg {
            margin: 1.5rem 0;
            display: flex;
            flex-direction: column;
            gap: 0.25rem;
        }
        .user-msg span {
            font-weight: 500;
            color: #1e293b;
            font-size: 0.875rem;
        }
        .user-msg .msg-content {
            padding: 0.8rem 1.2rem;
            border-radius: 1.2rem;
            max-width: 70%;
            margin-top: 0.3rem;
            font-size: 0.9rem;
            line-height: 1.5;
        }
        
        .user-msg[dir="ltr"] .msg-content {
            background: var(--message-in);
            color: #1e293b;
            border-radius: 1.2rem 1.2rem 1.2rem 0;
        }
        
        .user-msg[dir="rtl"] .msg-content {
            background: var(--message-out);
            color: white;
            border-radius: 1.2rem 1.2rem 0 1.2rem;
            margin-left: auto;
        }
        .user-msg[dir="rtl"] .msg-content {
            border-radius: 1rem 1rem 0 1rem;
            margin-left: auto;
        }
        .media-preview {
            max-width: 240px;
            margin: 0.5rem 0;
            border-radius: 0.5rem;
            box-shadow: 0 1px 3px 0 rgba(0,0,0,0.1), 0 1px 2px 0 rgba(0,0,0,0.06);
        }
        #emojiPanel {
            display: none;
            margin: 1rem 0;
            padding: 1.25rem;
            background: #ffffff;
            border-radius: 1rem;
            box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -2px rgba(0,0,0,0.1);
            display: grid;
            grid-template-columns: repeat(10, 1fr);
            gap: 0.75rem;
        }
        #emojiPanel span {
            cursor: pointer;
            font-size: 1.25rem;
            transition: transform 0.2s ease;
        }
        #emojiPanel span:hover {
            transform: scale(1.1);
        }

        .chat-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        #logoutBtn {
            background: var(--primary-color);
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            transition: background 0.3s;
        }

        #logoutBtn:hover {
            background: #1d4ed8;
        }
    </style>
</head>
<body>
    <div id="loginForm">
        <h2 style="color: #1e293b; font-size: 1.5rem; margin-bottom: 1.5rem;">加入实时聊天室</h2>
            <div style="margin-bottom: 2rem;">
                <input type="text" id="username" placeholder="输入您的昵称" style="width: 100%; padding: 0.9rem 1.2rem; border: 1px solid var(--border); border-radius: 0.8rem; font-size: 0.9rem; transition: border-color 0.3s ease;">
            </div>
        <!-- <input type="text" id="username" placeholder="用户名"> -->
        <button onclick="window.joinChat()">进入聊天室</button>
        <p id="errorMsg" style="color: red;"></p>
    </div>

    <div id="chatContainer">
        <div class="chat-header"><h2>实时聊天室</h2><button id="logoutBtn" class="btn">退出</button></div>
        <div id="messages"></div>
        <div class="input-group" style="gap: 0.8rem; align-items: center;">
            <button class="emoji-btn" onclick="showEmojiPanel()" style="border-radius: 1rem; padding: 0.8rem;">😀</button>
            <input type="file" id="mediaUpload" accept="image/*,video/*" style="display:none" onchange="handleMediaUpload()">
            <button class="media-btn" onclick="document.getElementById('mediaUpload').click()" style="border-radius: 1rem; padding: 0.8rem;">📎</button>
            <input type="text" id="messageInput" placeholder="输入消息/选择表情/上传媒体..." onkeypress="if(event.key==='Enter') sendMessage()"
                   style="flex: 1; border: 1px solid var(--border); border-radius: 1.5rem; padding: 0.9rem 1.2rem; font-size: 0.9rem; transition: all 0.3s ease;">
            <button onclick="sendMessage()" style="border-radius: 1.5rem; padding: 0.9rem 1.5rem;">发送</button>
        </div>
        <div id="emojiPanel" style="display:none; margin: 10px 0; padding: 15px; background: #fff; border-radius: 12px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
            😀 😃 😄 😁 😆 😅 😉 😊 😇 😍
            😘 😗 😙 😚 😋 😛 😝 😜 🤪 🤨
            🤩 🥳 🎉 👏 👍 👎 ✌️ ✊ 🤝 🙌
        </div>
    </div>

    <script src="http://localhost:3000/socket.io/socket.io.js"></script>
    <script>
        const socket = io('http://localhost:3000');
        let currentUser = '';

        function joinChat() {
            const username = document.getElementById('username').value.trim();
            if (!username) {
                alert('请输入用户名');
                return;
            }
            socket.emit('join', username, (response) => {
                if (response.success) {
                    currentUser = username;
                    document.getElementById('loginForm').style.display = 'none';
                    document.getElementById('chatContainer').style.display = 'block';
                } else {
                    document.getElementById('errorMsg').textContent = response.message;
                }
            });
        }

        function sendMessage() {
            const messageInput = document.getElementById('messageInput');
            const message = messageInput.value.trim();
            if (message) {
                socket.emit('chatMessage', { type: 'text', content: message });
                messageInput.value = '';
            }
        }

        function showEmojiPanel() {
            const panel = document.getElementById('emojiPanel');
            panel.style.display = panel.style.display === 'none' ? 'block' : 'none';
            // 点击表情插入输入框
            panel.onclick = (e) => {
                if (e.target.textContent.match(/[😀😃😄😁😆😅😉😊😇😍😘😗😙😚😋😛😝😜🤪🤨🤩🥳🎉👏👍👎✌️✊🤝🙌]/)) {
                    messageInput.value += e.target.textContent;
                }
            }
        }

        document.getElementById('logoutBtn').addEventListener('click', () => {
            socket.emit('logout', currentUser);
            currentUser = '';
            document.getElementById('loginForm').style.display = 'block';
            document.getElementById('chatContainer').style.display = 'none';
            document.getElementById('username').value = '';
        });

        function handleMediaUpload() {
            const fileInput = document.getElementById('mediaUpload');
            const file = fileInput.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = (e) => {
                    const mediaType = file.type.startsWith('image/') ? 'image' : 'video';
                    socket.emit('chatMessage', {
                        type: mediaType,
                        content: e.target.result,
                        name: file.name
                    });
                };
                reader.readAsDataURL(file);
                fileInput.value = ''; // 清空输入框以便重复选择
            }
        }

        socket.on('message', (data) => {
            const messagesDiv = document.getElementById('messages');
            const msgElement = document.createElement('div');
            msgElement.className = 'user-msg';
            let contentHtml;
            switch(data.message.type) {
                case 'text':
                    contentHtml = `<div class="msg-content">${data.message.content}</div>`;
                    break;
                case 'image':
                    contentHtml = `<div class="msg-content"><img class="media-preview" src="${data.message.content}" alt="${data.message.name}"></div>`;
                    break;
                case 'video':
                    contentHtml = `<div class="msg-content"><video class="media-preview" controls src="${data.message.content}"></video></div>`;
                    break;
                default:
                    contentHtml = `<div class="msg-content">[不支持的消息类型]</div>`;
            }
            msgElement.innerHTML = `<span>${data.username}</span>: ${contentHtml}`;
            messagesDiv.appendChild(msgElement);
            messagesDiv.scrollTop = messagesDiv.scrollHeight;
        });

        socket.on('userJoined', (msg) => {
            const messagesDiv = document.getElementById('messages');
            const msgElement = document.createElement('div');
            msgElement.className = 'system-msg';
            msgElement.textContent = msg;
            messagesDiv.appendChild(msgElement);
            messagesDiv.scrollTop = messagesDiv.scrollHeight;
        });

        socket.on('userLeft', (msg) => {
            const messagesDiv = document.getElementById('messages');
            const msgElement = document.createElement('div');
            msgElement.className = 'system-msg';
            msgElement.textContent = msg;
            messagesDiv.appendChild(msgElement);
            messagesDiv.scrollTop = messagesDiv.scrollHeight;
        });
    </script>
</body>
</html>